<?php
class UserLog {
	public function addUserLog($userName,$module,$method,$params){
			$param = json_decode($params);
			switch($module){
				case "tuan":
					$moduleId = $param->teamId;
					break;
				case 'product':
					if($method=='getProduct'){
						$moduleId = $param->id;
					}else if($method=='getProductByTagId'){
						$moduleId = $param->tagId;
						$module='getProductByTagId';
					}
					break;
				default:
					$moduleId = $param->id;
					break;
			}
			if(!$userName||!$moduleId){return;}
			$fromUserName=$param->fromUserName;
			$url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	    	$date = date('Y-m-d H:i:s');
	    	$UserLog = D('user_log');
			$shareLog = D('share_log');
//			$shareStatistics = D("share_statistics");
			$customerId = Session::$user['customerId'];
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$mishare = D('mishare');
			Session::switchCustomer($customerId);

//		/** 判断是否存在mishare表*/
		$sharewhere['module']=array("eq",$module);
		$sharewhere['moduleId']=array("eq",$moduleId);
//		$sharewhere['cid']=array("eq",$customerId);
		$mishareInfo = $mishare->where($sharewhere)->find();
		if(!$mishareInfo){
			/** 不存在添加到mishare表并浏览人数,次数加 1 */
			$mishare->create();
			$mishare ->module = $module;
			$mishare ->moduleId = $moduleId;
			$mishare ->viewUv = 1;
			$mishare ->viewCount = 1;
			$mishare ->shareCount = 0;
			$mishare ->shareUv = 0;
			$mishare ->commentCount = 0;
			$mishare ->praiseCount = 0;
			$mishare ->createTime = date('Y-m-d H:i:s');
			$mishare ->cid = $customerId;
			$mishare->add();
		}
//		else{
//			$where['module']=array("eq",$module);
//			$where['moduleId']=array("eq",$moduleId);
//			$where['userName']=array("eq",$userName);
//			$LogResult = $UserLog->where($where)->find();
//			if(!empty($LogResult)){
//				$mishare->where($sharewhere)->setInc("viewCount");
//			}else{
//				/** 存在浏览人数,次数加 1 */
//				$mishare->where($sharewhere)->setInc("viewUv");
//				$mishare->where($sharewhere)->setInc("viewCount");
//			}
//
//		}


//			/** 判断之前是否浏览过*/
//			$where['module']=array("eq",$module);
//			$where['moduleId']=array("eq",$moduleId);
//			$where['userName']=array("eq",$userName);
//			$LogResult = $UserLog->where($where)->find();
//			if(!$LogResult){
//				/** 判断是否存在mishare表*/
//				$sharewhere['module']=array("eq",$module);
//				$sharewhere['moduleId']=array("eq",$moduleId);
//				$mishare = $mishare->where($sharewhere)->find();
//				if(!$mishare){
//					/** 不存在添加到mishare表并浏览人数,次数加 1 */
//					$mishare->create();
//					$mishare ->module = $module;
//					$mishare ->moduleId = $moduleId;
//					$mishare ->viewUv = 1;
//					$mishare ->viewCount = 1;
//					$mishare ->shareCount = 0;
//					$mishare ->shareUv = 0;
//					$mishare ->commentCount = 0;
//					$mishare ->praiseCount = 0;
//					$mishare->add();
//				}else{
//
//				}

//				$shareStaReuslt = $shareStatistics->where($sharewhere)->find();
//				if(!$shareStaReuslt){
//					$shareStatistics->create();
//					$shareStatistics ->module = $module;
//					$shareStatistics ->moduleId = $moduleId;
//					$shareStatistics ->uv = 1;
//					$shareStatistics ->createTime = $date;
//					$shareStatistics->add();
//				}else{
//					$shareStatistics->where($sharewhere)->setInc("uv");
//				}
//			}
			if($fromUserName){
				$where['fromUserName']=array("eq",$fromUserName);
				$where['module']=array("eq",$module);
				$where['moduleId']=array("eq",$moduleId);
				$where['userName']=array("eq",$userName);
				$LogResult = $UserLog->where($where)->find();
				if(!$LogResult){
					unset($where['userName']);
					$shareResult = $shareLog->where($where)->find();
					if(!$shareResult){
						$shareLog->create();
						$shareLog->fromUserName=$fromUserName;
						$shareLog ->module = $module;
						$shareLog ->moduleId = $moduleId;
						$shareLog ->uv = 1;
						$shareLog ->createTime = $date;
						$shareLog->add();
					}else{
						$shareLog->where($where)->setInc("uv");
					}
				}
			}
			$data['fromUserName'] = $fromUserName;
			$data['userName']=$userName;
			$data['moduleId'] = $moduleId;
			$data['params'] = $params;
			$data['module']= $module;
			$data['createTime'] = $date;
			$data['url'] = $url;
			$id = $UserLog->add($data);
			return;
	}
	
	private function addPageViewCount($url){
		    $PageViewCount = D('page_view_count');
		    $where['url']=$url;
		    $res=$PageViewCount->where($where)->find();
		    if(!isset($res)){
				    $PageViewCount ->url =$url;
				    $PageViewCount ->count =1;
				    $result = $PageViewCount->add();
		    }else{ 
		    		$data["count"] = $res["count"]+1;
		    		$result = $PageViewCount->where($where)->save($data);
		    }
		    return $result;
	}
	
}